package com.auctionhaus

import grails.test.*

class BidControllerTests extends ControllerUnitTestCase {
    transient Date testFutureDate =  new Date()+1;
    transient Date testCurrentDate =  new Date();

    protected void setUp() {
        super.setUp()

        def seller = new Customer(emailAddress:"GenericEmail@gmail.com",
                                  password:"Password",
                                  date: testCurrentDate);
        def listing = new Listing(creationDate:testCurrentDate,
                                  listName:"2011 Jeep Grand Cherokee",
                                  endDateTime:testFutureDate,
                                  startBidPrice:15000,
                                  seller:seller)
        mockDomain(Customer,[seller])
        mockDomain(Listing, [listing])

    }

    protected void tearDown() {
        super.tearDown()
    }
    //L-7: The detail page for the listing allows a new bid to be placed (unit test)
    //Julie: I asked Mike Hugo, if we needed to do something special to indicate this came
    // from the listing/show.gsp page...
    // He responded, no we don't need to do anything special to indicate where the request is coming from
    void testCreateBidFromListingPage() {
        def buyer = new Customer(emailAddress:"BuyerEmail@gmail.com",
                                  password:"Password",
                                  date: testCurrentDate);

        def myListing = Listing.findByListName("2011 Jeep Grand Cherokee")
        mockDomain(Customer, [buyer])
        mockDomain(Bid)
        mockParams.amount = 15001
        mockParams.buyerEmailAddress = buyer.emailAddress;
        mockParams.selectedListName =  myListing.listName

        controller.metaClass.message = {args -> println "bid saved: ${args}"}


        controller.save()
        def myBid = Bid.findByBuyerAndAmount(buyer, 15001)
        assertEquals('show', redirectArgs.action)
        assertEquals(myBid.id, redirectArgs.id)

    }
    void testCreateBidAttemptWithInvalidBuyer() {
        def myListing = Listing.findByListName("2011 Jeep Grand Cherokee")
        mockDomain(Customer)
        mockDomain(Bid)
        mockParams.amount = 15001
        mockParams.buyerEmailAddress = "julie@dakotarichter.com";
        mockParams.selectedListName =  myListing.listName

        controller.metaClass.message = {args -> println "bid not saved: ${args}"}


        controller.save()
        assertEquals('julie@dakotarichter.com is not a registered AuctionHaus user.', controller.flash.message)
        assertEquals('create', renderArgs.view)
    }

    void testCreateBidAttemptWithInvalidAmount() {
        def myListing = Listing.findByListName("2011 Jeep Grand Cherokee")
        def buyer = new Customer(emailAddress:"BuyerEmail@gmail.com",
                                          password:"Password",
                                          date: testCurrentDate);

        mockDomain(Customer, [buyer])
        mockDomain(Bid)
        mockParams.amount = 15000.49
        mockParams.buyerEmailAddress =  buyer.emailAddress;
        mockParams.selectedListName =  myListing.listName

        controller.metaClass.message = {args -> println "bid not saved: ${args}"}


        controller.save()
        assertTrue controller.flash.message.startsWith("Bid Amount must be at least")
        assertEquals('create', renderArgs.view)
    }

    //UI-4: The action of placing a new bid will display a message to the user indicating that the bid was successful (Controller Unit Test)
    void testBidPlacedDisplaysSuccessfulMsg() {

        def myListing = Listing.findByListName("2011 Jeep Grand Cherokee")
        def buyer = new Customer(emailAddress:"BuyerEmail@gmail.com",
                                          password:"Password",
                                          date: testCurrentDate);

        mockDomain(Customer, [buyer])
        mockDomain(Bid)
        mockParams.amount = 15001
        mockParams.buyerEmailAddress =  buyer.emailAddress;
        mockParams.selectedListName =  myListing.listName

        controller.placeBid()

        assertEquals('Bid Placed Successfully.', controller.flash.message)
        assertEquals('/listing/showWinningBid', renderArgs.template)
    }

    void testBidPlacedDisplaysUnSuccessfulBuyerMsg() {

        def myListing = Listing.findByListName("2011 Jeep Grand Cherokee")

        mockDomain(Customer)
        mockDomain(Bid)
        mockParams.amount = 15001
        mockParams.buyerEmailAddress = "julieganz@dakotarichter.com";
        mockParams.selectedListName =  myListing.listName

        controller.placeBid()

        assertEquals('Bid Rejected. julieganz@dakotarichter.com is not a registered AuctionHaus user.', controller.flash.message)
        assertEquals('/listing/showWinningBid', renderArgs.template)
    }

    void testBidPlacedDisplaysUnSuccessfulBidAmountMsg() {

        def myListing = Listing.findByListName("2011 Jeep Grand Cherokee")
        def buyer = new Customer(emailAddress:"julieganz@dakotarichter.com",
                                          password:"Password",
                                          date: testCurrentDate);

        mockDomain(Customer, [buyer])
        mockDomain(Bid)
        mockParams.amount = 15000.49
        mockParams.buyerEmailAddress =  buyer.emailAddress;
        mockParams.selectedListName =  myListing.listName

        controller.placeBid()

        assertTrue controller.flash.message.startsWith("Bid Rejected. Bid Amount must be at least")
        assertEquals('/listing/showWinningBid', renderArgs.template)
    }

}
